package com.yikaiye.android.yikaiye.ui.activity.mine;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityBillDetailsBinding;
import com.yikaiye.android.yikaiye.di.retrofit.ApiConstants;
import com.yikaiye.android.yikaiye.model.InvoiceDetailsModel;
import com.yikaiye.android.yikaiye.ui.activity.mine.viewmodel.CompanyDetailsViewModel;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.pictrue.GlideEngine;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 发票详情
 */
public class BillDetailsActivity extends BaseActivity<ActivityBillDetailsBinding, CompanyDetailsViewModel> {

    private int type;
    private List<InvoiceDetailsModel.FileModel> fileList = new ArrayList<>();

    @Override
    protected CompanyDetailsViewModel bindModel() {
        return getViewModel(this, CompanyDetailsViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.invoice_details));
        if (getIntent().getExtras() != null) {
            String id = getIntent().getExtras().getString(Constant.ID_KEY);
            viewModel.getInvoiceDetails(id);
        }
    }

    @Override
    protected void initClick() {
        viewModel.invoiceDetailsModelMutableLiveData.observe(this, invoiceDetailsModel -> {
            if (invoiceDetailsModel != null) {
                if (invoiceDetailsModel.getType() != null) {
                    binding.billAmountTv.setText(context.getString(R.string.price, invoiceDetailsModel.getType() == 0 || invoiceDetailsModel.getType() == 1 ? invoiceDetailsModel.getInvoiceMoney() : invoiceDetailsModel.getSjInvoiceMoney()));
                    type = invoiceDetailsModel.getType();
                    if (invoiceDetailsModel.getType() != null) {
                        // 类型（0-未审核，1.已审核，2.已开票，3.已红冲，4.已作废）
                        if (invoiceDetailsModel.getType() == 0 || invoiceDetailsModel.getType() == 1) {
                            binding.iv.setImageResource(R.mipmap.icon_billing);
                            binding.billStatusTv.setText(getString(R.string.waiting_bill));
                            binding.billDesTv.setText(getString(R.string.invoice_examining));
                            binding.billStatusTv.setTextColor(ContextCompat.getColor(context, R.color.color_3d90f2));
                            binding.billDesTv.setTextColor(ContextCompat.getColor(context, R.color.color_3d90f2));
                        } else if (invoiceDetailsModel.getType() == 2) {
                            binding.iv.setImageResource(R.mipmap.icon_already_invoice);
                            binding.billStatusTv.setText(context.getString(R.string.already_invoice));
                            binding.billDesTv.setVisibility(View.GONE);
                            binding.billStatusTv.setTextColor(ContextCompat.getColor(context, R.color.color_46b39e));
                        } else if (invoiceDetailsModel.getType() == 3) {
                            binding.iv.setImageResource(R.mipmap.icon_read_washed);
                            binding.billStatusTv.setText(context.getString(R.string.red_washed));
                            binding.billDesTv.setText(getString(R.string.red_money, invoiceDetailsModel.getBcInvoiceMoney()));
                            binding.billStatusTv.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));
                            binding.billDesTv.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));
                        } else {
                            binding.iv.setImageResource(R.mipmap.icon_invalid);
                            binding.billStatusTv.setText(context.getString(R.string.bill_invalid));
                            binding.billDesTv.setVisibility(View.GONE);
                            binding.billStatusTv.setTextColor(ContextCompat.getColor(context, R.color.color_929292));
                        }
                    }
                }
                if (invoiceDetailsModel.getInvoiceType() != null) {
                    if (invoiceDetailsModel.getInvoiceType() == 1) {
                        binding.billTypeTv.setText(getString(R.string.e_common_invoice));
                    } else if (invoiceDetailsModel.getInvoiceType() == 1) {
                        binding.billTypeTv.setText(getString(R.string.e_special_invoice));
                    } else {
                        binding.billTypeTv.setText(getString(R.string.ding_e_invoice));
                    }
                }
                binding.billTitleTv.setText(CommonUtil.checkText(invoiceDetailsModel.getInvoiceTitle()));
                binding.companyCodeTv.setText(CommonUtil.checkText(invoiceDetailsModel.getDutyParagraph()));
                binding.billBodyTv.setText(CommonUtil.checkText(invoiceDetailsModel.getSubjectName()));
                binding.applyTimeTv.setText(CommonUtil.checkText(invoiceDetailsModel.getApplyCreateTime()));
                fileList = invoiceDetailsModel.getFileList();
                if (fileList == null || invoiceDetailsModel.getFileList().isEmpty()) {
                    binding.lookBillLl.setVisibility(View.GONE);
                }
            }
        });
        viewModel.onDelayClick(binding.lookBillLl, o -> {
            if (type == 3) { // 只有红冲是图片
                ArrayList<LocalMedia> mData = new ArrayList<>();
                for (InvoiceDetailsModel.FileModel fileModel : fileList) {
                    LocalMedia localMedia = new LocalMedia();
                    localMedia.setPath(ApiConstants.BASE_PDF_URL + fileModel.getFilePath());
                    mData.add(localMedia);
                }
                PictureSelector.create(context)
                        .openPreview()
                        .setImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                        .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                            @Override
                            public void onPreviewDelete(int position) {

                            }

                            @Override
                            public boolean onLongPressDownload(Context context, LocalMedia media) {  // 长按保存图片
                                return false;
                            }
                        })
                        .startActivityPreview(0, false, mData);
            } else {
                Bundle bundle = new Bundle();
                bundle.putSerializable(Constant.MODEL_KEY, (Serializable) fileList);
                JumpIntent.jump(this, PdfActivity.class, bundle);
            }

//            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(ApiConstants.BASE_PDF_URL + invoiceUrl));
//            startActivity(browserIntent);
        });
    }
}